package panel.viewSpots.navigation
{
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	import main.ClientConstants;
	
	public class NavigationSpotGroup extends Sprite
	{
		private var _lock:Boolean;
		private var _clickTime:int;
		private var _currentSpot:NavigationSpot;
		private var _cancelBtn:Sprite;
		private var _rightBtn:Sprite;
		private var _loader:ResourceLoader;
		private var _uri:URI;
		private var _changePathFunc:Function;
		private var _paopaoComp:PaoPaoComp;
		private var _centerFunc:Function;
		private var _playMusic:Function;
		private var _spotList:Array = [];
		private var _circleIcon:Bitmap;
		private var _allTime:uint;
		private var _onOpenDetail:Function;
		public function NavigationSpotGroup()
		{
			super();
		}
		
		public function init(dataList:Array,loader:ResourceLoader,uri:URI,recSwf:Sprite,musicFunc:Function,changePath:Function,stage:Stage):void
		{
			_playMusic = musicFunc;
			_changePathFunc = changePath;
			_uri = uri;
			_loader = loader;
			var tempSpot:Object;
			var spr:Sprite;
			for(var i:int=0;i<dataList.length;i++)
			{
				spr = recSwf["spot" + i];
				if(!spr) continue;
				tempSpot = dataList[i];
				tempSpot.posY = tempSpot.posY + 1.5 * ClientConstants.HOME_UP_PER * stage.stageHeight;
				var spotItem:NavigationSpot = new NavigationSpot();
				spotItem.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
				spotItem.addEventListener(MouseEvent.MOUSE_UP,upHandler);
				spotItem.show(tempSpot,loader,uri,spr);
				this.addChild(spotItem);
				_spotList.push(spotItem);
			}
			
			_cancelBtn = new Sprite();
			_rightBtn = new Sprite();
			this.addChild(_cancelBtn);
			this.addChild(_rightBtn);
			
			_rightBtn.addEventListener(MouseEvent.CLICK,clickAddBtn);
			_cancelBtn.addEventListener(MouseEvent.CLICK,clickCancelHandler);
			
			_paopaoComp = new PaoPaoComp(_loader);
			this.addChild(_paopaoComp);
			_paopaoComp.visible = false;
			_paopaoComp.x = _paopaoComp.width/2;
			_paopaoComp.y = _paopaoComp.height/2
			_paopaoComp.onMusic = _playMusic;
			_paopaoComp.onDetail = _onOpenDetail;
			
			_circleIcon = _loader.getImage("circle");
			_circleIcon.visible = false;
			this.addChild(_circleIcon);
		}
		
		public function changePathArrow(pathList:Array):void
		{
			_allTime = 0;
			var spotLen:int = _spotList.length;
			var len:int = pathList.length;
			var spot:NavigationSpot;
			var isFind:Boolean;
			
			for(var j:int = 0;j < spotLen;j ++)
			{
					spot = _spotList[j];
					isFind = false;
		       	var roadNum:int = 0;
				for(var i:int=0;i<len;i++)
				{
					if(pathList[i].nodeType == 1)
					{
						++　roadNum;
					}
					if(pathList[i].index == spot.data.id)
					{
						spot.changeArrow(true,i - roadNum);
						_allTime = _allTime + spot.data.time;
						isFind = true;
						break;
					}
				}
				if(!isFind)
				{
					spot.changeArrow(false,0);
				}
			}
		}
		
		public function get allSpotTime():uint
		{
			return _allTime;
		}
		
		public function showCircleIco(index:String = null):void
		{
			if(index == null)
			{
			_circleIcon.visible = false;
			return;
			}else
			{
				var spot:NavigationSpot = seekSpot(index);
				if(spot == null) return;
				_circleIcon.x = spot.x;
				_circleIcon.y = spot.y;
				_circleIcon.visible = true;
			}
			
		}
		
		private function seekSpot(index:String):NavigationSpot
		{
			for(var i:int=0;i<_spotList.length;i++)
			{
				if(_spotList[i].data.id == index)
				{
					return _spotList[i];
				}
			}
			return null;
		}
		
		protected function clickCancelHandler(event:MouseEvent):void
		{
			clearShakeBtns();
			_changePathFunc(false,_currentSpot.spotID);
		}
		
		protected function clickAddBtn(event:MouseEvent):void
		{
			clearShakeBtns();
			_changePathFunc(true,_currentSpot.spotID);
		}
		
		private function clearShakeBtns():void
		{
			if(_currentSpot)
			{
				_currentSpot.stopShake();
			}
			_rightBtn.visible = false;
			_cancelBtn.visible = false;
		}
		
		protected function upHandler(event:MouseEvent):void
		{
			if(_lock) return;
			_lock = true;
			clearShakeBtns();
			_clickTime = getTimer() - _clickTime;
			_currentSpot = event.currentTarget as NavigationSpot;
			if(_clickTime > 500)
			{
				if(_cancelBtn.numChildren == 0)
				{
					_cancelBtn.addChild(_loader.getImage(_uri.getPublicRec("x")));
					_rightBtn.addChild(_loader.getImage(_uri.getPublicRec("right")));
					var scale:Number = 0.5;
					_cancelBtn.scaleX = _cancelBtn.scaleY = scale;
					_rightBtn.scaleX = _rightBtn.scaleY = scale;
					_cancelBtn.x = _currentSpot.x + _currentSpot.width/2 - _cancelBtn.width/2;
					_cancelBtn.y = _currentSpot.y - _cancelBtn.height;
					_rightBtn.x = _currentSpot.x  - _rightBtn.width/2 - _currentSpot.width/2;
					_rightBtn.y = _currentSpot.y - _rightBtn.height;
				}else
				{
					_cancelBtn.x = _currentSpot.x + _currentSpot.width/2 - _cancelBtn.width/2;
					_cancelBtn.y = _currentSpot.y - _cancelBtn.height;
					_rightBtn.x = _currentSpot.x  - _rightBtn.width/2 - _currentSpot.width/2;
					_rightBtn.y = _currentSpot.y - _rightBtn.height;
				}
				_cancelBtn.visible = true;
				_rightBtn.visible = true;
				
				_currentSpot.shake();
			}else
			{
				_centerFunc( _currentSpot.x,_currentSpot.y);
				resizePaoPao();
				trace("触发景点详细介绍");
			}
		}
		
		private function resizePaoPao():void
		{
			if(_currentSpot.y - _paopaoComp.height- _currentSpot.height / 2 < 0)
			{
				_paopaoComp.changeScale(0,-1);
				_paopaoComp.y = _currentSpot.y +  _currentSpot.height / 2 + _paopaoComp.height / 2;
			}else
			{
				_paopaoComp.changeScale(0,1);
				_paopaoComp.y = _currentSpot.y - _paopaoComp.height/2 - _currentSpot.height / 2;
			}
			_paopaoComp.x = _currentSpot.x + (_currentSpot.width - _paopaoComp.width) / 2 - _currentSpot.width / 2 + _paopaoComp.width / 2;
			_paopaoComp.show(_currentSpot.data);
			_paopaoComp.visible = true;
		}
		
		public function hidePoaPao():void
		{
			if(_paopaoComp && _paopaoComp.visible == true)
			{
				_paopaoComp.visible = false;
			}
		}
		
		public function set onOpenDetail(value:Function):void
		{
			_onOpenDetail = value;
		}
		
		
		public function set centerFunc(value:Function):void
		{
			_centerFunc = value;
		}
		
		protected function downHandler(event:MouseEvent):void
		{
			_lock = false;
			_clickTime = getTimer();
		}
		
	}
}